package com.zhai.domain.rebate.service;

import com.zhai.domain.award.model.valobj.TaskStateVO;
import com.zhai.domain.rebate.event.SendRebateMessageEvent;
import com.zhai.domain.rebate.model.aggregate.BehaviorRebateAggregate;
import com.zhai.domain.rebate.model.entity.BehaviorEntity;
import com.zhai.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import com.zhai.domain.rebate.model.entity.TaskEntity;
import com.zhai.domain.rebate.model.valobj.BehaviorTypeVO;
import com.zhai.domain.rebate.model.valobj.DailyBehaviorRebateVO;
import com.zhai.domain.rebate.repository.IBehaviorRebateRepository;
import com.zhai.types.common.Constants;
import com.zhai.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 行为返利接口实现类
 * @ClassName: BehaviorRebateService
 * @Author: zhaiyongxin
 * @Date: 2025/5/27 13:36
 * @Version: 1.0
 */
@Service
@Slf4j
public class BehaviorRebateService implements IBehaviorRebateService {

    @Resource
    private IBehaviorRebateRepository behaviorRebateRepository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;

    /**
     * 创建行为动作的入账订单
     *
     * @param behaviorEntity
     * @return
     */
    @Override
    public List<String> createOrder(BehaviorEntity behaviorEntity) {
        BehaviorTypeVO behaviorTypeVO = behaviorEntity.getBehaviorTypeVO();
        // 1.根据行为查询返利配置
        List<DailyBehaviorRebateVO> dailyBehaviorRebateList = behaviorRebateRepository.queryDailyBehaviorRebateConfig(behaviorTypeVO);
        // 2.遍历返利配置操作
        List<String> orders = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();

        String userId = behaviorEntity.getUserId();
        String outBusinessNo = behaviorEntity.getOutBusinessNo();
        for (DailyBehaviorRebateVO dailyBehaviorRebateVO : dailyBehaviorRebateList) {
            // 2.1 组装业务主键： 用户id_返利类型_业务主键
            String bizId = userId + Constants.UNDERLINE + dailyBehaviorRebateVO.getRebateType() + Constants.UNDERLINE + outBusinessNo;
            // 2.2 组装行为返利订单实体对象
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = BehaviorRebateOrderEntity.builder()
                    .orderId(RandomStringUtils.randomNumeric(11))
                    .behaviorType(dailyBehaviorRebateVO.getBehaviorType())
                    .rebateConfig(dailyBehaviorRebateVO.getRebateConfig())
                    .rebateDesc(dailyBehaviorRebateVO.getRebateDesc())
                    .rebateType(dailyBehaviorRebateVO.getRebateType())
                    .bizId(bizId)
                    .outBusinessNo(outBusinessNo)
                    .userId(userId)
                    .build();
            orders.add(behaviorRebateOrderEntity.getOrderId());
            // 2.3 组装Task实体对象
            SendRebateMessageEvent.SendRebateMessage sendRebateMessage = SendRebateMessageEvent.SendRebateMessage.builder()
                    .bizId(bizId)
                    .rebateType(dailyBehaviorRebateVO.getRebateType())
                    .rebateConfig(dailyBehaviorRebateVO.getRebateConfig())
                    .rebateDesc(dailyBehaviorRebateVO.getRebateDesc())
                    .userId(userId)
                    .build();
            BaseEvent.EventMessage<SendRebateMessageEvent.SendRebateMessage> rebateMessageEventMessage = this.sendRebateMessageEvent.buildEventMessage(sendRebateMessage);
            TaskEntity taskEntity = TaskEntity.builder()
                    .topic(sendRebateMessageEvent.topic())
                    .state(TaskStateVO.create)
                    .messageId(rebateMessageEventMessage.getId())
                    .message(rebateMessageEventMessage)
                    .userId(userId)
                    .build();
            // 2.4 组装聚合对象
            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .userId(userId)
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .taskEntity(taskEntity)
                    .build();
            behaviorRebateAggregates.add(behaviorRebateAggregate);
        }
        // 3.存储聚合对象数据
        behaviorRebateRepository.saveUserRebateRecord(userId, behaviorRebateAggregates);

        // 4.返回订单id集合
        return orders;
    }

    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return behaviorRebateRepository.queryOrderByOutBusinessNo(userId, outBusinessNo);
    }
}
